package cn.mygweb.common.pojo;

import java.io.Serializable;
import java.util.List;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

/** 
* <p>Title: JsonResult.java</p>
* <p>Description:  自定义响应数据结构，状态码：<br>
* 					200：表示成功
* 					500：表示错误，错误信息在msg字段中
* 					501：bean验证错误，不管多少个错误都以map形式返回
* 					502：拦截器拦截到用户token出错
* 					555：异常抛出信息
* </p>  
* @author gxb 
* @date 2019年5月18日 
* <p>Copyright: Copyright (c) 2019</p>
* <p>Company: www.mygweb.cn</p> 
*/ 
public class JsonResult implements Serializable{

	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = -1681420627055980731L;
	
	/**
	 * 	响应业务状态码
	 */
	private Integer status;
	/**
	 * 	响应的消息
	 */
	private String msg;
	/**
	 * 	响应的数据
	 */
	private Object data;
	/**
	 * 	定义jackson对象
	 */
	private static final ObjectMapper MAPPER = new ObjectMapper();
	
	public JsonResult() {
		super();
	}
	
	public JsonResult(Integer status, String msg, Object data) {
		super();
		this.status = status;
		this.msg = msg;
		this.data = data;
	}
	
	public JsonResult(Object data) {
		this.status = 200;
		this.msg = "Ok";
		this.data = data;
	}
	
	/**
	 * <p>Title: build</p>
	 * <p>Description: 自定义返回数据</p>
	 * @param status
	 * @param msg
	 * @param data
	 * @return
	 */
	public static JsonResult build(Integer status, String msg, Object data) {
		return new JsonResult(status, msg, data);
	}
	
	/**
	 * <p>Title: ok</p>
	 * <p>Description: 返回200状态码，无数据</p>
	 * @return
	 */
	public static JsonResult ok() {
		return new JsonResult(null);
	}
	
	/**
	 * <p>Title: ok</p>
	 * <p>Description: 返回200状态码，并返回数据</p>
	 * @param data
	 * @return
	 */
	public static JsonResult ok(Object data) {
		return new JsonResult(data);
	}
	
	/**
	 * <p>Title: errorMsg</p>
	 * <p>Description: 返回500状态码以及错误信息</p>
	 * @param msg
	 * @return
	 */
	public static JsonResult errorMsg(String msg) {
		return new JsonResult(500, msg, null);
	}
	
	/**
	 * <p>Title: errorMap</p>
	 * <p>Description: 返回501状态码，并返回数据</p>
	 * @param data
	 * @return
	 */
	public static JsonResult errorMap(Object data) {
		return new JsonResult(501, "error", data);
	}
	
	/**
	 * <p>Title: errorTokenMsg</p>
	 * <p>Description: 返回502状态码以及错误信息</p>
	 * @param msg
	 * @return
	 */
	public static JsonResult errorTokenMsg(String msg) {
		return new JsonResult(502, msg, null);
	}
	
	/**
	 * <p>Title: errorException</p>
	 * <p>Description: 返回555状态码以及错误信息</p>
	 * @param msg
	 * @return
	 */
	public static JsonResult errorException(String msg) {
		return new JsonResult(555, msg, null);
	}
	
	/**
	 * <p>Title: isOk</p>
	 * <p>Description: 是否成功</p>
	 * @return
	 */
	public boolean isOk() {
		return this.status == 200;
	}
	
	/**
	 * <p>Title: formatJsonToPojo</p>
	 * <p>Description: 将json结果集转化为JsonResult对象，需要转换的对象是一个类</p>
	 * @param JsonData
	 * @param clazz
	 * @return
	 */
    public static JsonResult formatJsonToPojo(String jsonData, Class<?> clazz) {
        try {
            if (clazz == null) {
                return MAPPER.readValue(jsonData, JsonResult.class);
            }
            JsonNode jsonNode = MAPPER.readTree(jsonData);
            JsonNode data = jsonNode.get("data");
            Object obj = null;
            if (clazz != null) {
                if (data.isObject()) {
                    obj = MAPPER.readValue(data.traverse(), clazz);
                } else if (data.isTextual()) {
                    obj = MAPPER.readValue(data.asText(), clazz);
                }
            }
            return build(jsonNode.get("status").intValue(), jsonNode.get("msg").asText(), obj);
        } catch (Exception e) {
            return null;
        }
    }
	
	/**
	 * <p>Title: formatJson</p>
	 * <p>Description: 没有Object对象的转化</p>
	 * @param JsonData
	 * @return
	 */
    public static JsonResult formatJson(String json) {
        try {
            return MAPPER.readValue(json, JsonResult.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    
    /**
     * <p>Title: formatJsonToList</p>
     * <p>Description: Object是集合转化，需要转换的对象是一个list</p>
     * @param jsonData
     * @param clazz
     * @return
     */
    public static JsonResult formatJsonToList(String jsonData, Class<?> clazz) {
        try {
            JsonNode jsonNode = MAPPER.readTree(jsonData);
            JsonNode data = jsonNode.get("data");
            Object obj = null;
            if (data.isArray() && data.size() > 0) {
                obj = MAPPER.readValue(data.traverse(),
                        MAPPER.getTypeFactory().constructCollectionType(List.class, clazz));
            }
            return build(jsonNode.get("status").intValue(), jsonNode.get("msg").asText(), obj);
        } catch (Exception e) {
            return null;
        }
    }

	public Integer getStatus() {
		return status;
	}

	public void setStatus(Integer status) {
		this.status = status;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public Object getData() {
		return data;
	}

	public void setData(Object data) {
		this.data = data;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	} 
	
}
